import 'package:flutter/material.dart';
import 'package:ride_rex/standard/app_colors.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_android/webview_flutter_android.dart';
import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

class WebPage extends StatefulWidget {
  const WebPage({super.key, required this.url, this.title = ''});

  final String url;
  final String title;

  @override
  State<WebPage> createState() => _WebPageState();
}

class _WebPageState extends State<WebPage> {
  late final WebViewController _controller;

  final ValueNotifier<int> _progress = ValueNotifier<int>(0);
  final ValueNotifier<String> _title = ValueNotifier('');

  @override
  void initState() {
    super.initState();

    late final PlatformWebViewControllerCreationParams params;
    if (WebViewPlatform.instance is WebKitWebViewPlatform) {
      params = WebKitWebViewControllerCreationParams(
        allowsInlineMediaPlayback: true,
        mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
      );
    } else {
      params = const PlatformWebViewControllerCreationParams();
    }

    final WebViewController controller = WebViewController.fromPlatformCreationParams(params);

    controller
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(Colors.white)
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) async {
            _progress.value = progress;
            _title.value = await controller.getTitle() ?? '';
          },
          onPageFinished: (String url) {
            debugPrint('Page finished loading: $url');
          },
        ),
      )
      ..loadRequest(Uri.parse(widget.url));

    if (controller.platform is AndroidWebViewController) {
      AndroidWebViewController.enableDebugging(true);
      (controller.platform as AndroidWebViewController).setMediaPlaybackRequiresUserGesture(false);
    }

    _controller = controller;
  }

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder(
      valueListenable: _title,
      builder: (BuildContext context, String value, Widget? child) {
        return BaseScaffold(
          titleStr: widget.title.getAuto(value.getAuto(' ')),
          child: Stack(
            children: [
              WebViewWidget(controller: _controller),
              WebViewProgress(_progress),
            ],
          ),
        );
      },
    );
  }
}

///进度条
class WebViewProgress extends StatelessWidget {
  const WebViewProgress(this.progress, {Key? key}) : super(key: key);

  final ValueNotifier<int> progress;

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: progress,
      builder: (context, _) {
        if (progress.value >= 100) return const SizedBox.shrink();
        return LinearProgressIndicator(
          value: progress.value.toDouble(),
          backgroundColor: Colors.transparent,
          valueColor: const AlwaysStoppedAnimation(AppColors.primary),
          minHeight: 3,
        );
      },
    );
  }
}
